/**
 * Module dependencies.
 */
//ejs config
var ejs = require('ejs');
ejs.open = '[%';
ejs.close = '%]';

// Configuration
var express = require('express');
var app = express();

var MemoryStore = express.session.MemoryStore;
var storeMemory = new MemoryStore({
    reapInterval:60000 * 10
});
var sessionConfig = express.session({
    key:'sid',
    store:storeMemory
});

function app_common_configure() {
    //设置监听端口
    app.set('port', process.env.PORT || 3000);
    //配置模板引擎
    app.set('views', __dirname + '/views');
    app.engine('html', ejs.__express);
    app.set('view engine', 'html');
    //常规配置
    app.use(express.favicon());
    app.use(express.logger('dev'));
    app.use(express.bodyParser());
    app.use(express.methodOverride());
    app.use(express.cookieParser('itravel'));
    //配置session
    app.use(sessionConfig);
    //配置路由
    app.use(app.router);
    //配置静态目录
    app.use(express.static(__dirname));
}

app.configure(app_common_configure);

app.configure('development', function () {
    app.use(express.errorHandler({
        dumpExceptions:true,
        showStack:true
    }));
});

app.configure('production', function () {
    app.use(express.errorHandler());
});

// Routes
var initRoute = require('./routes');
initRoute(app);

//app start
app.listen(3000, function () {
    console.log("Express server listening on port %d in %s mode", app.get('port'), app.get('env'));
});